/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package umled.model;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Observable;
import org.w3c.dom.*;
/**
 *
 * @author ANor
 */
public abstract class UMLElement extends Observable{
    private String name;
    private int xPos;
    private int yPos;
    private int width;
    private int height;
    private UMLElement parent;
        
    public UMLElement(String s, UMLElement parent, int xPos, int yPos, int width, int height){
        name = s;
        this.xPos = xPos;
        this.yPos = yPos;
        this.width = width;
        this.height = height;
        this.parent = parent;
    }
    public String getName(){
        return name;
    }
    public void setName(String s){
        name = s;
    }
    public int getX(){
        return xPos;
    }
    public int getY(){
        return yPos;
    }
    public void setXY(int x, int y){
        xPos = x;
        yPos = y;
    }
    public void setWidth(int w){
        width = w;
    }
    public void setHeight(int h){
        height = h;
    }
    public int getWidth(){
        return width;
    }
    public int getHeight(){
        return height;
    }
    public abstract void draw(Graphics2D g2d, int x, int y);
    public boolean contains(int x, int y){
        return new Rectangle( getX(), getY(), getWidth(), getHeight()).contains(x, y);
    }
    public void drawString( Graphics2D g2d, String s, int x, int y, int limX, int limY){
        if( y <= limY)
            g2d.drawString( s.substring(0, Math.min((limX - x)/7 + 1, s.length())), x, y);
    }
    public void drawRectangle( Graphics2D g2d, int x0, int y0, int x, int y, int limX, int limY){
        if( x > limX)
            x = limX;
        if( y > limY)
            y = limY;
        g2d.drawRect(x0, y0, x - x0, y - y0);
    }
    
    /**
     * Returns XML-represention of current element
     * @param document to which this element will be appended
     * @return XML-represention of current element
     */
    public Element getXML( Document document ){
        Element element = document.createElement( "UMLElement" );
        
        Element temp = document.createElement( "Name" );
        temp.appendChild( document.createTextNode( name ) );
        element.appendChild( temp );
        
        temp = document.createElement( "X Pos" );
        temp.appendChild( document.createTextNode( String.valueOf( xPos ) ) );
        element.appendChild( temp );
        
        temp = document.createElement( "Y Pos" );
        temp.appendChild( document.createTextNode( String.valueOf( yPos ) ) );
        element.appendChild( temp );
        
        temp = document.createElement( "Widht" );
        temp.appendChild( document.createTextNode( String.valueOf( width ) ) );
        element.appendChild( temp );
        
        temp = document.createElement( "Height" );
        temp.appendChild( document.createTextNode( String.valueOf( height ) ) );
        element.appendChild( temp );
        
        return element;
    }
    /**
     * Returns parent element for current element. If current element is UMLModel, returns null.
     * @return parent element
     */
    public UMLElement getParent(){
        return parent;
    }
 }
